﻿using BySoft.Server.Business;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class MaterialSmallTypeEdit : AuthBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlPartType.Attributes.Add("readonly", "true");
                InitDDL();
                BindData();
            }
            Master.ValidationGroup = "gSubmit";
        }
        void InitDDL()
        {

            DataTable dt = MdMaterialtype.GetMdMaterialtypeAll().Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                this.ddlPartType.DataSource = dt;
                this.ddlPartType.DataTextField = "MaterialTypeName";
                this.ddlPartType.DataValueField = "MaterialTypeID";
                this.ddlPartType.DataBind();
            }


            UIHelper.InsertFirstItem(ddlPartType);

            //BindParameterForDDL("ModuleType", ddlPartType);

            //UIHelper.InsertFirstItem(ddlPartType);
        }
        private void BindData()
        {
            string strCode = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id").Trim());
            if (!string.IsNullOrEmpty(strCode))
            {
                MdMaterialsmalltypeEntity en = MdMaterialsmalltype.GetMdMaterialsmalltypeByID(strCode);
                if (en != null)
                {
                    this.lblMaterialSmallTypeID.Text = en.MaterialSmallTypeID;
                    this.txtMaterialSmallTypeName.Text = en.MaterialSmallTypeName;
                    //this.chkIsHalfFinished.Checked = en.IsHalfFinished.Equals("1") ? true : false;
                    this.ddlPartType.SelectedValue = en.MaterialTypeID;

                   
                }
            }
        }
       

      

       

      

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(CurrentUser.UserID))
                {
                    UIHelper.AlertMessage(this.Page, "等待时间太久，请重新填写资料！");
                    return;
                }
                if (!string.IsNullOrEmpty(this.txtMaterialSmallTypeName.Text.Trim()))
                {
                    if ((new MdMaterialsmalltype()).IsNameExists(txtMaterialSmallTypeName.Text.Trim(), lblMaterialSmallTypeID.Text))
                    {
                        UIHelper.AlertMessage(this.Page, "原料小类编号已存在，请重新填写！");
                        return;
                    }
                    MdMaterialsmalltypeEntity entity = new MdMaterialsmalltypeEntity();
                   


                    entity.MaterialSmallTypeID = this.lblMaterialSmallTypeID.Text.Trim();
                    entity.MaterialSmallTypeName = this.txtMaterialSmallTypeName.Text.Trim();
                    entity.MaterialTypeID = ddlPartType.SelectedValue.Trim();

                    entity.Status = "1";
                    entity.ModifyDate = DateTime.Now;
                    entity.ModifyUserID = CurrentUser.UserID;
                    entity.ModifyUserName = CurrentUser.UserName;
                    
                    if (MdMaterialsmalltype.Update(entity))
                    {
                        base.LogAction(BaseEnum.ActionType.Modify, entity.MaterialSmallTypeID, true, null, new EntityBase[] { entity });
                        UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../MasteData/MaterialSmallTypeList.aspx");
                    }
                    else
                    {
                        UIHelper.AlertMessage(this.Page, Message.SaveFail);
                    }
                }

            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, FomatString(ex.Message.ToString()));
            }
        }
    }
}